﻿using Notifications.Wpf;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using VideoTools.Extension;
using VideoTools.Util;
using VideoTools.vo;

namespace VideoTools.SubtitleView
{
    /// <summary>
    /// MargeM3U8Page.xaml 的交互逻辑
    /// </summary>
    public partial class SplitSubtitlePage : Page
    {
        private static volatile SplitSubtitlePage _instance;
        private static object _syncRoot = new object();
        private string subtitleFile;
        private string subtitleText;
        private string fileExt = string.Empty;
        private List<string> txts = new List<string>();
        private static readonly string autoMergeEndWith = "__分离{0:G}__timeline_out";
        public static SplitSubtitlePage Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new SplitSubtitlePage();
                        }

                    }
                }
                return _instance;
            }
        }


        public SplitSubtitlePage()
        {
            InitializeComponent();
            
        }

        
        private void OpenSubtitleFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "字幕文件(*.srt)|*.srt";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                subtitleFile = openFileDialog.FileName;
                var fileContent = Utils.ReadFile(subtitleFile);
                subtitleText = fileContent.content;
                SubtitleEditer.Text = subtitleText;
                txts = fileContent.lines;
            }
            else
            {
                return;
            }
        }
        
        private void ReloadSubtitleFile_Click(object sender, RoutedEventArgs e)
        {
            if (subtitleFile != null && subtitleFile.Length > 0)
            {
                var fileContent = Utils.ReadFile(subtitleFile);
                subtitleText = fileContent.content;
                SubtitleEditer.Text = subtitleText;
                txts = fileContent.lines;
            }
        }

        private void SplitSubtitle_Click(object sender, RoutedEventArgs e)
        {
            this.txts.Clear();
            this.txts = SubtitleEditer.GetLines();
            var checkResult = SubtitleUtils.checkSubtitleDue(this.txts);
            SubTitleError error = checkResult.error;
            int lineNumber = checkResult.line;
            bool check = true;
            if (error != SubTitleError.OK)
            {
                check = false;
                var content = new NotificationContent()
                {
                    Title = "似乎字幕文件格式有点问题,原因",
                    Type = NotificationType.Error,
                    Message = "行数:" + lineNumber + "\n" + error.Desc(),
                };
                AppManager.Instance.NotificationManager.Show(content);
                int lineCount = SubtitleEditer.LineCount;
                if (lineNumber > 0 && lineNumber <= lineCount)
                {
                    SubtitleEditer.GotoLineAndHightLight(lineNumber);
                }
            }
            if (!check)
            {
                return;
            }
            var result = SubtitleUtils.ReadDueSubtitle(txts);
            string name2 = System.IO.Path.GetFileName(subtitleFile);
            name2 = name2.Replace(fileExt, "");
            string filePath = subtitleFile.Replace(fileExt, "-" + name2 + autoMergeEndWith + fileExt);
            List<Subtitle> aList = result.subtitleList1;
            List<Subtitle> bList = result.subtitleList2;
            List<List<Subtitle>> list = new List<List<Subtitle>>();
            list.Add(aList);
            list.Add(bList);
            for (int i = 0; i < list.Count; i++)
            {
                string path = string.Format(filePath, i);
                using (StreamWriter writer = new StreamWriter(path))
                {
                    foreach (Subtitle subtitle in list[i])
                    {
                        writer.WriteLine(subtitle.Number);
                        writer.WriteLine($"{subtitle.StartTime:hh\\:mm\\:ss\\,fff} --> {subtitle.EndTime:hh\\:mm\\:ss\\,fff}");
                        writer.WriteLine(subtitle.Text);
                        writer.WriteLine();
                    }
                }
            }
            SubtitleEditer.Save(subtitleFile);
        }
    }
}
